package com.kfm.jdbc.day0324;

import java.sql.*;
import java.util.List;

/**
 * 数据库操作的工具类
 * 提供操作数据库 --> getConnection()提供数据库连接
 * executeUpdate() 执行sql
 * executeQuery() 执行查询的sql
 */
public class DBUtils<T> {

    private String url = "jdbc:mysql://localhost:3306/store";
    private String user = "root";
    private String password = "";
    private Connection connection;

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public DBUtils() {
    }

    public DBUtils(String url, String user, String password) {
        this.url = url;
        this.user = user;
        this.password = password;
    }

    private Connection getConnection() throws ClassNotFoundException, SQLException {
        if (connection == null || connection.isClosed()) { // 第一次创建DBUtils对象或者 connection已经被关闭
            Class.forName("com.mysql.cj.jdbc.Driver");
            connection = DriverManager.getConnection(url, user, password);
        }
        return connection;
    }

    /**
     * 修改，删除，新增
     *
     * @param sql 执行的sql语句
     * @return 受影响行数
     * @throws SQLException           创建SQL执行对象异常
     * @throws ClassNotFoundException 找不到驱动类
     */
    //public int executeUpdate(String sql) throws SQLException, ClassNotFoundException {
    //    getConnection();
    //    Statement statement = connection.createStatement();
    //    return statement.executeUpdate(sql);
    //}
    //
    //public ResultSet executeQuery(String sql) throws SQLException, ClassNotFoundException {
    //    getConnection();
    //    Statement statement = connection.createStatement();
    //    return statement.executeQuery(sql);
    //}
    public int executeUpdate(String sql, Object... values) throws SQLException, ClassNotFoundException {
        getConnection();
        PreparedStatement statement = connection.prepareStatement(sql);
        //statement.setObject(1, "123");
        //statement.setString(2, "");
        //for (int i = 0; i < values.length; i++) {
        //    // 替换占位符
        //    statement.setObject(i + 1, values[i]);
        //}
        setObject(statement, values);
        return statement.executeUpdate();
    }

    public List<T> executeQuery(String sql,Recover<T> recover, Object... values) throws SQLException, ClassNotFoundException {
        getConnection();
        PreparedStatement statement = connection.prepareStatement(sql);
        setObject(statement, values);
        return recover.recover(statement.executeQuery());
    }

    /**
     * 替换占位符
     *
     * @param statement PreparedStatement 对象
     * @param values    要替换的值
     */
    private void setObject(PreparedStatement statement, Object... values) throws SQLException {
        if (values == null) {
            return;
        }
        for (int i = 0; i < values.length; i++) {
            // 替换占位符
            statement.setObject(i + 1, values[i]);
        }
    }

    public void close() throws SQLException {
        if (connection != null && !connection.isClosed()) {
            connection.close();
            connection = null;
        }
    }

}
